package org.jground.rx.ui.zk;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.IRXObject;
import org.jground.rx.RXClass;
import org.jground.rx.RXObjectService;
import org.jground.rx.RXSystem;
import org.jground.rx.RXView;
import org.jground.rx.ui.IUIAttribute;
import org.jground.rx.ui.zk.viewControl.UIAttributeFactory;
import org.jground.util.L;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Button;


public class UIViewSynopsis implements EventListener {
    //
    //
    //
    private static Log logger = LogFactory.getLog(UIViewSynopsis.class);
    //
    //
    // 
    protected UIFrame uiFrame;
    protected RXClass moClass;
    //protected IRXObject rxDataObject;
    protected List<IUIAttribute<Component,UIFrame>> uiAttributes = new ArrayList<IUIAttribute<Component,UIFrame>>();
    protected List<Component> panels = new ArrayList<Component>();
    protected List<Component> panelsWithLink = new ArrayList<Component>();
    protected List<Button> links = new ArrayList<Button>();
    protected Component typeIcon;


    public UIViewSynopsis(UIFrame uiFrame, String moid) {
        this.uiFrame = uiFrame;
        this.moClass = RXSystem.getInstance().getRxService().getRXClass(moid);
        init();
    }

    protected  void init() {
        L.debug(logger,"Start Init of UIViewSynopsis", this.moClass.getMoid());
        typeIcon = UIStyles.getInstance().getTypeIcon(this.moClass.getMoid());
        panels.add(typeIcon);
        // panelsWithLink.add(UIStyles.getInstance().getTypeIcon(this.moClass.getMoid()));
        panelsWithLink.add(typeIcon);
        for (RXView moView : moClass.getSynopsis().getRXViews()) {
            IUIAttribute<Component,UIFrame> uiAttribute = UIAttributeFactory.create(uiFrame, moView);
            uiAttributes.add(uiAttribute);
            //
          //  panelsWithLink.add(new ZDiv(UIStyles.SYNOPSIS, CreateAndRegisterLink().Set(uiAttribute.GetControlComponent())));
            //
          //  panels.add(new ZDiv(UIStyles.SYNOPSIS, uiAttribute.GetControlComponent()));
        }
    }
    public void onEvent(Event he) {
        if(he.getTarget() instanceof Button) {
            Button link = (Button)he.getTarget();
            IRXObject mod = RXObjectService.getInstance().getById(link.getId());
            if(mod != null) {
               // uiFrame.OpenDialogOnStack(new UIViewWindowDelegator(uiFrame.getViewWindow(mod.getMoid()), mod));
            }
        }
    }
    
//    private Button CreateAndRegisterLink() {
//        Button link = new Button();
//        //link.setSclass(UIStyles.VIEW_LINK);
//        link.addEventListener(Events.ON_CLICK,this);
//        links.add(link);
//        return link;
//    }

//    public  void RenderTo(IRXObject mod,  List<HContainer> comps) {
//        //this.moDataObject = mod;
//        if(mod.getDataState() == RXDataState.DELETED || mod.getDataState() == RXDataState.DELETED_UNAPPROVED) {
//            //typeIcon.set , "text-decoration:line-through");
//        } else {
//            //typeIcon.setAttribute(HDTD.AttName.sTYLE, "");
//        }
//
//        for (IUIAttribute<Component,UIFrame> uiAttribute : uiAttributes) {
//            uiAttribute.clearData();
//            uiAttribute.setData(mod);
//        }
//        List<Component,UIFrame> ps = null;
//        if(!mod.getMOClass().isComponent() && RXAccess.getInstance().canRead(mod)) {
//            ps = panelsWithLink;
//            for (Button link : links) {
//                link.setId(String.valueOf(mod.getOid()));
//            }
//        } else {
//            ps = panels;
//        }
//        //
////        for(int i = 0; i < comps.size(); i++) {
////            if(i < ps.size()) {
////                comps.get(i).set(ps.get(i));
////            } else {
////                comps[i].set(nbsp);
////            }
////        }
//    }
}
